package com.education.wisdom.controller.mq;


import com.education.wisdom.pojo.LeaveMessage;
import com.education.wisdom.service.LeaveMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 测试消费
 */
//@RestController
public class Producer {

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Logger logger = LoggerFactory.getLogger(getClass());

    //由于ConfigurableBeanFactory.SCOPE_PROTOTYPE配置 无法自动注入 需要构造方法注入
    private RabbitTemplate rabbitTemplate;

    @Autowired
    public Producer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Autowired
    private LeaveMessageService leaveMessageService;


    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage(LeaveMessage leaveMessage) {
        //添加数据库
        leaveMessage.setCreatetime(new Date());
        leaveMessageService.insertLeaveMessage(leaveMessage);
        //实时推送消息
        Map<String, Object> map = new HashMap<>();
        map.put("content",leaveMessage.getContent());
        //将消息携带绑定键值：wisdom_routingkey_message  发送到交换机wisdom_exchange_message
        rabbitTemplate.convertAndSend("wisdom_exchange_message", "wisdom_routingkey_message", map);
        return "ok";
    }

    /**
     * 测试延时队列
     * @param message
     * @return
     */
    @GetMapping("/sendDelayMessage")
    public String sendDelayMessage(String message) {
        logger.info("发送消息到延时队列 : {} --- 时间 : {}",message,simpleDateFormat.format(new Date()));
        rabbitTemplate.convertAndSend("wisdom_exchange_message", "wisdom_routingkey_delay", message);
        return "success";
    }

}
